package com.kensure.shike.order.service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import co.kensure.frame.JSBaseService;
import co.kensure.mem.CollectionUtils;
import co.kensure.mem.MapUtils;

import com.kensure.basekey.BaseKeyService;
import com.kensure.shike.order.dao.SKOrderBackDao;
import com.kensure.shike.order.model.SKOrderBack;


/**
 * 订单退货图片 服务实现类
 * @author fankd created on 2019-7-7
 * @since 
 */
@Service
public class SKOrderBackService extends JSBaseService{
	
	@Resource
	private SKOrderBackDao dao;
	@Resource
	private BaseKeyService baseKeyService;
    
    
    public SKOrderBack selectOne(Long id){
    	return dao.selectOne(id);
    }
	
	public List<SKOrderBack> selectByIds(Collection<Long> ids){
		return dao.selectByIds(ids);
	}
	
	public List<SKOrderBack> selectAll(){
		return dao.selectAll();
	}
	
	public List<SKOrderBack> selectByWhere(Map<String, Object> parameters){
		return dao.selectByWhere(parameters);
	}
	
	
	public long selectCount(){
		return dao.selectCount();
	}
	
	public long selectCountByWhere(Map<String, Object> parameters){
		return dao.selectCountByWhere(parameters);
	}
	
	
	public boolean insert(SKOrderBack obj){
		super.beforeInsert(obj);
		obj.setId(baseKeyService.getKey("sk_order_back"));
		return dao.insert(obj);
	}
	
	public boolean insertInBatch(List<SKOrderBack> objs){
		return dao.insertInBatch(objs);
	}
	
	
	public boolean update(SKOrderBack obj){
		return dao.update(obj);
	}
    
    public boolean updateByMap(Map<String, Object> params){
		return dao.updateByMap(params);
	}
    
    
	public boolean delete(Long id){
		return dao.delete(id);
	}	
	
    public boolean deleteMulti(Collection<Long> ids){
		return dao.deleteMulti(ids);
	}
    
    public boolean deleteByWhere(Map<String, Object> parameters){
		return dao.deleteByWhere(parameters);
	}
    
	/**
	 * 获取宝贝的图片列表
	 * 
	 * @param bbid
	 * @return
	 */
	public List<SKOrderBack> getList(Long orderid) {
		Map<String, Object> parameters = MapUtils.genMap("orderid", orderid, "orderby", "id");
		List<SKOrderBack> list = selectByWhere(parameters);
		return list;
	}

	public boolean add(String[] pics,Long orderid) {
		// 删除老的
		List<SKOrderBack> oldlist = getList(orderid);
		if (CollectionUtils.isNotEmpty(oldlist)) {
			List<Long> ids = new ArrayList<>();
			for (SKOrderBack word : oldlist) {
				ids.add(word.getId());
			}
			deleteMulti(ids);
		}
		// 插入新的
		for (String url : pics) {
			SKOrderBack back = new SKOrderBack();
			back.setOrderid(orderid);
			back.setUrl(url);
			insert(back);
		}
		return true;
	}
  

}
